package com.zaofan.mobile.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.exception.ServiceException;
import com.zaofan.mobile.bean.Account;
import com.zaofan.service.AccountService;
import com.zaofan.service.PrepaidService;
import com.zaofan.utils.JsonUtils;

@WebServlet("/mobile/prepaid")
public class PrepaidController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;
	private PrepaidService prepaidService = new PrepaidService();
    private AccountService accountService = new AccountService();

	protected void prepaid(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String card = request.getParameter("card");
			String password = request.getParameter("password");
			String accounId = request.getParameter("accountId");
			prepaidService.recharge(card, password, accounId);
            Account account = accountService.findAccountInfo(Integer.valueOf(accounId));
            request.getSession().setAttribute("userinfo",account);
			JsonUtils.writeSuccessJson("充值卡充值成功", "", response);
			return;
		}catch (ServiceException e) {
			JsonUtils.writeBeanFailJson(e.getMessage(), response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("充值卡充值错误", response);
		}
		JsonUtils.writeBeanFailJson("充值卡充值失败", response);
	}

}
